import { FIELD_TYPE, FILTER_TYPE } from '@/framework/types/portal.d'
import { useDictStore } from '@/framework/store/common'
import { PORTAL_SUPPORT_CONDITION_DICT } from '@/framework/constant'

const dict = useDictStore()
export const getRelation = async (type: FIELD_TYPE | string) => {
  return dict
    .getDict(PORTAL_SUPPORT_CONDITION_DICT, undefined, (data: Array<any>) => {
      for (const item of data) {
        // 因为 getDict 函数中只涉及到 value 和 label，所以此时处理到的结果（数组）只能放到label中进行保存
        item.label = item.label.split(',').map((item: string) => {
          const [value, label] = item.split(':')
          return { value, label, text: label }
        })
      }
      return data
    })
    .then(() => dict.getMap(PORTAL_SUPPORT_CONDITION_DICT).valueMap.get(type))
}

export const getDefaultFilterType = (fieldType: FIELD_TYPE, strict = false) => {
  switch (fieldType) {
    case FIELD_TYPE.INPUT:
      return strict ? FILTER_TYPE.EQUAL : FILTER_TYPE.LIKE
    case FIELD_TYPE.SWITCH:
      return FILTER_TYPE.EQUAL
    case FIELD_TYPE.NUMBER:
      return FILTER_TYPE.EQUAL
    case FIELD_TYPE.SELECT:
      return FILTER_TYPE.IN
    case FIELD_TYPE.TREE:
      return FILTER_TYPE.IN
    case FIELD_TYPE.DATE:
      return FILTER_TYPE.BETWEEN
    case FIELD_TYPE.DATETIME:
      return FILTER_TYPE.BETWEEN
    case FIELD_TYPE.SELECT_MULTI_IN_ONE:
      return FILTER_TYPE.CONTAIN_IN
    case FIELD_TYPE.TREE_MULTI_IN_ONE:
      return FILTER_TYPE.CONTAIN_IN
    default:
      return strict ? FILTER_TYPE.EQUAL : FILTER_TYPE.LIKE
  }
}
